<?php
namespace app\push\controller;

use app\push\logic\Trc20Logic;
use think\Db;
use think\worker\Server;
use Workerman\Lib\Timer;

class Worker extends Server
{
    protected $socket = 'websocket://0.0.0.0:2346';
    protected $processes = 1;

    /**
     * 当连接建立时触发的回调函数
     * @param $connection
     */
    public function onConnect($connection)
    {

    }

    /**
     * 收到信息
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        $connection->send('我收到你的信息了');
    }


    /**
     * 当连接断开时触发的回调函数
     * @param $connection
     */
    public function onClose($connection)
    {

    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }


    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {

        $trc =  new Trc20Logic();

        $project = Db::name('project')->where('status','Y')
            ->select();

        foreach ($project as $item) {
            // 转账监听定时器
            Timer::add(180,[$trc,"checkTransaction"],[$item['id']],true);
            // 归集定时器
            Timer::add(60 * 60 * 4,[$trc,"collection"],[$item['id']],true);
        }
    }
}